package com.dgut.staffmodule.web;

import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dgut.common.util.ExtAjaxResponse;
import com.dgut.common.util.ExtJsonResult;
import com.dgut.common.util.ExtPageable;
import com.dgut.staffmodule.entity.Department;
import com.dgut.staffmodule.entity.DepartmentEditDTO;
import com.dgut.staffmodule.entity.DepartmentQueryDTO;
import com.dgut.staffmodule.service.IDepartmentService;

@Controller
@RequestMapping("/department")
public class DepartmentController {

	@Autowired
	private IDepartmentService departmentService;// 部门service

	/**
	 * 保存部门信息
	 *
	 * @param department
	 *            部门信息
	 */
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Department department) {
		try {
			departmentService.saveOrUpdate(department);
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 保存部门信息
	 *
	 * @param departmentEditDTO
	 *            部门信息
	 */
	@RequestMapping("/save")
	public @ResponseBody ExtAjaxResponse save(DepartmentEditDTO departmentEditDTO) {
		try {
			departmentService.save(departmentEditDTO);
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 保存更改信息
	 *
	 * @param departmentEditDTO
	 *            部门信息
	 */
	@RequestMapping("/update")
	public @ResponseBody ExtAjaxResponse update(DepartmentEditDTO departmentEditDTO) {
		try {
			departmentService.update(departmentEditDTO);
			return new ExtAjaxResponse(true, "操作成功！");
		} catch (Exception e) {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 删除一个部门
	 *
	 * @param id
	 *            前端传的部门id
	 */
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse delete(@RequestParam Long id) {

		Department department = departmentService.findOne(id);
		if (departmentService.delete(department)) {
			return new ExtAjaxResponse(true, "操作成功！");
		} else {
			return new ExtAjaxResponse(false, "删除失败，该部门存在员工，若删除，请移除其下员工");
		}
	}

	/**
	 * 批量删除部门
	 *
	 * @param ids
	 *            前端传的部门ids
	 */
	@RequestMapping("/deleteDepartments")
	public @ResponseBody ExtAjaxResponse deleteDepartments(@RequestParam Long[] ids) {
		List<Long> idLists = Arrays.asList(ids);
		if (departmentService.delete(idLists)) {
			return new ExtAjaxResponse(true, "操作成功！");
		} else {
			return new ExtAjaxResponse(false, "操作失败！");
		}
	}

	/**
	 * 查找一个部门
	 *
	 * @param id
	 *            前端传的部门id
	 */
	@RequestMapping("/findOne")
	public @ResponseBody Department findOne(@RequestParam Long id) {
		Department department = departmentService.findOne(id);

		return department;
	}

	/**
	 * 查找所有部门
	 */
	@RequestMapping("/findAll")
	public @ResponseBody ExtJsonResult<Department> findAll() {
		List<Department> deptLists = departmentService.findAll();

		return new ExtJsonResult<Department>(deptLists);
	}

	/**
	 * 分页查找部门
	 *
	 * @param departmentQueryDTO
	 *            前端传的部门信息departmentQueryDTO
	 * @param pageable
	 *            分页条件
	 */
	@RequestMapping("/findPage")
	public @ResponseBody Page<Department> findPage(DepartmentQueryDTO departmentQueryDTO, ExtPageable pageable) {
		/*
		 * List<Department> deptLists = departmentService.findAll(); for(Department d :
		 * deptLists) { int count = staffService.findByDeptId(d.getId());
		 * d.setCount(count); System.out.println(d.getCount()); }
		 */
		Page<Department> page = departmentService.findAll(DepartmentQueryDTO.getSpecification(departmentQueryDTO),
				pageable.getPageable());
		return page;
	}
}
